using Cysharp.Threading.Tasks;
using ShawnFramework;
using UnityEngine;

public class Enemy : MonoBehaviour
{
    [Header("移动速度")] 
    public float moveSpeed = 4f;
    
    private Transform _target;
    private PlayerController _player;
    
    private void Start()
    {
        _target = NodeUtil.GetFirstComponentByLayer<Transform>("Player");
        _player = _target.GetComponent<PlayerController>();
    }

    private void Update()
    {
        Move();
        Attack();
    }

    private void Move()
    {
        var curPos = transform.position;
        var targetPos = _target.position;
        transform.position = Vector3.MoveTowards(curPos, targetPos, moveSpeed * Time.deltaTime);
    }

    private void Attack()
    {
        var curPos = transform.position;
        var targetPos = _target.position;
        if (Vector3.SqrMagnitude(curPos - targetPos) <= _player.hurtRadius)
        {
            // 对玩家造成伤害
            TweenUtil.Discolored(
                _player.playerSprite, 
                Color.red,
                Color.white,
                0.2f
                ).Forget();
            // 销毁自己
            if (Pool.Global.CheckIsExsit(PoolElemTypeEnum.Enemy00))
            {
                ObjectPool.Global.Recycle(PoolElemTypeEnum.Enemy00, gameObject);
            }
        }
    }
 }
